package com.nari.ysphd.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremyfeinstein.gson.Meeting;
import com.jeremyfeinstein.utils.ConfigUtil;
import com.nari.ysphd.R;
import com.nari.ysphd.base.AbsAdapter;
import com.nari.ysphd.base.AbsViewHolder;
import com.nari.ysphd.listener.OnForthMeetingListener;
import com.nari.ysphd.listener.OnGoingMeetingListener;
import com.nari.ysphd.util.TextUtils;

/**
 * Create by sunxunchao on 2019/3/29
 */
public class ForthcomingMeetingsAdapter extends AbsAdapter<Meeting, ForthcomingMeetingsAdapter.OngoingMeetingsViewHolder> {

    private OnForthMeetingListener onForthMeetingListener;

    public ForthcomingMeetingsAdapter(Context context, int layoutId) {
        super(context, layoutId);
    }

    public void setOnForthMeetingListener(OnForthMeetingListener onForthMeetingListener) {
        this.onForthMeetingListener = onForthMeetingListener;
    }

    private String valueOf(Object obj) {
        return (obj == null) ? "" : obj.toString();
    }

    @Override
    protected void initData(final Meeting meeting, final OngoingMeetingsViewHolder holder, int position) {

        holder.room_name.setText(valueOf(meeting.getMeetingName()));
        holder.room_main_member.setText(TextUtils.desensitization(valueOf(meeting.getHostName())));
        holder.room_date.setText(String.format("%s ~ %s", meeting.getStartTime(), meeting.getEndTime()));
        holder.room_summary.setText(valueOf(meeting.getAgenda()));
        holder.room_number.setText(String.format("互动议程（参会人数:%s）", meeting.getCurrentNumbers()));

        if (!isMeetingHost(meeting) && "4".equals(meeting.getStatus())) {
            holder.room_name.setText(String.format("%s(已暂停)", meeting.getMeetingName()));
        }
        holder.edit_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onForthMeetingListener != null) {
                    onForthMeetingListener.onEditClicked(v, meeting);
                }
            }
        });

        holder.delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onForthMeetingListener != null) {
                    onForthMeetingListener.onDeleteClicked(v, meeting);
                }
            }
        });

        holder.rl_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.ll_content.getVisibility() == View.GONE) {
                    holder.ll_content.setVisibility(View.VISIBLE);
                } else {
                    holder.ll_content.setVisibility(View.GONE);
                }
            }
        });

        holder.delete_button.setVisibility(isMeetingHost(meeting) ? View.VISIBLE : View.GONE);
        holder.edit_button.setVisibility(isMeetingHost(meeting) ? View.VISIBLE : View.GONE);
    }

    //判断是不是互动主持人
    private boolean isMeetingHost(Meeting meeting) {
        return meeting.getHostCode().equals(ConfigUtil.getInstance().getLoginUser().getUSER_CODE())
                || meeting.getCreateCode().equals(ConfigUtil.getInstance().getLoginUser().getUSER_CODE());
    }

    @Override
    protected OngoingMeetingsViewHolder newViewHolder(int position) {
        return new OngoingMeetingsViewHolder();
    }

    static class OngoingMeetingsViewHolder extends AbsViewHolder {

        private TextView room_name, room_main_member, room_date, room_number;
        private Button delete_button, edit_button;
        private LinearLayout ll_content;
        private TextView room_summary;
        private LinearLayout rl_top;

        @Override
        public void initItemView(View containerView) {
            room_name = (TextView) containerView.findViewById(R.id.room_name);
            room_main_member = (TextView) containerView.findViewById(R.id.room_main_member);
            room_date = (TextView) containerView.findViewById(R.id.room_date);
            room_number = (TextView) containerView.findViewById(R.id.room_number);

            room_summary = (TextView) containerView.findViewById(R.id.room_summary);

            edit_button = (Button) containerView.findViewById(R.id.edit_button);
            delete_button = (Button) containerView.findViewById(R.id.delete_button);

            ll_content = (LinearLayout) containerView.findViewById(R.id.ll_content);
            rl_top = (LinearLayout) containerView.findViewById(R.id.rl_top);
        }
    }
}
